package com.audaque.springboot.foshanupload.mbmul.config;


import com.audaque.springboot.foshanupload.core.enums.DbType;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * @author zgb
 * 自定义SqlSessionFactoryBean时setDatabaseIdProvider实现多种数据库切换
 * @desc 因为mybatisplus的baseMapper的sql是动态生成的，所以无法在Mapper.xml中标注databaseId=？
 * <select id="findAuthorityByUrl" resultType="java.lang.String" databaseId="postgresql">
 * @date 2022-04-29 17:08:12
 */

@Configuration
@ConditionalOnProperty(value = "switch.mbmul", havingValue = "true", matchIfMissing = false)
public class DatabaseIdConfig {


    @Bean
    public DatabaseIdProvider databaseIdProvider() {
        DatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
        Properties p = new Properties();
        p.setProperty(DbType.MYSQL.name(), DbType.MYSQL.getDb());
        p.setProperty(DbType.GAUSS.name(), DbType.GAUSS.getDb());
        p.setProperty(DbType.DM.name(), DbType.DM.getDb());
        p.setProperty(DbType.ORACLE.name(), DbType.ORACLE.getDb());
        p.setProperty(DbType.POSTGRE_SQL.name(), DbType.POSTGRE_SQL.getDb());
        p.setProperty(DbType.SQL_SERVER.name(), DbType.SQL_SERVER.getDb());
        databaseIdProvider.setProperties(p);
        return databaseIdProvider;
    }


}
